﻿
//控制语句转化器
namespace n_ControlSence
{
using System;
using n_Code1;
using n_Deploy;
using n_ET;
using n_Expression;
using n_ParseNet;
using n_VarList;
using n_VarNode;
using n_VarType;
using n_WordList;
using n_ConstString;
using n_Config;

public static class ControlSence
{
	//控制语句转化器, 每个控制语句中只允许最多一个中间变量和布尔变量
	public static void 控制语句( int Index, int LastControlIndex )
	{
		++Deploy.VarLevel;
		++Deploy.SenceNumber;
		
		if( ParseNet.NodeSet[ Index ][ 0 ] == ParseNet.Node.如果语句 ) {
			if语句( Index, LastControlIndex );
		}
		else if( ParseNet.NodeSet[ Index ][ 0 ] == ParseNet.Node.如果否则语句 ) {
			ifelse语句( Index, LastControlIndex );
		}
		else if( ParseNet.NodeSet[ Index ][ 0 ] == ParseNet.Node.迭代语句 ) {
			for语句( Index );
		}
		else if( ParseNet.NodeSet[ Index ][ 0 ] == ParseNet.Node.loop语句 ) {
			loop语句( Index );
		}
		else if( ParseNet.NodeSet[ Index ][ 0 ] == ParseNet.Node.while语句 ) {
			while语句( Index );
		}
		else if( ParseNet.NodeSet[ Index ][ 0 ] == ParseNet.Node.dowhile语句 ) {
			dowhile语句( Index );
		}
		else if( ParseNet.NodeSet[ Index ][ 0 ] == ParseNet.Node.switch语句 ) {
			switch语句( Index );
		}
		else if( ParseNet.NodeSet[ Index ][ 0 ] == ParseNet.Node.多分支如果语句 ) {
			switchelse语句( Index );
		}
		else if( ParseNet.NodeSet[ Index ][ 0 ] == ParseNet.Node.反复执行语句 ) {
			//MessageBox.Show( "注意: [反复执行语句] 已经过时, 请尽快修改您的代码!" );
			repeat语句( Index );
		}
		else if( ParseNet.NodeSet[ Index ][ 0 ] == ParseNet.Node.forever语句 ) {
			forever语句( Index );
		}
		else if( ParseNet.NodeSet[ Index ][ 0 ] == ParseNet.Node.单次执行语句 ) {
			once语句( Index );
		}
		else if( ParseNet.NodeSet[ Index ][ 0 ] == ParseNet.Node.用户流程语句 ) {
			用户流程语句( Index );
		}
		else {
			ET.ShowError( "<控制语句> 未定义的控制语句: " + ParseNet.NodeSet[ Index ][ 0 ] );
		}
		--Deploy.VarLevel;
		VarList.ClearUselessVar( Deploy.VarLevel );
	}
	
	//====================================================================
	
	static void switch语句( int Index )
	{
		int ControlIndex = Deploy.SenceNumber;
		Expression.AddIns( Code1.语句层加 );
		int ExSenceIndex = int.Parse( ParseNet.NodeSet[ Index ][ 3 ] );
		int VarIndex = 计算整型表达式( ExSenceIndex );
		string MidType = VarList.Get( VarIndex ).Type;
		if( MidType != VarType.BaseType.Uint8 ) {
			ET.WriteParseError( VarList.Get( VarIndex ).Location, "当前版本语法限定switch的表达式应是一个uint8类型: " + MidType );
			return;
		}
		case分支判断( Index, ControlIndex, VarIndex );
		Expression.AddIns( Code1.标号 + " " + Config.PreLabel.End + ControlIndex );
		Expression.AddIns( Code1.语句层减 );
	}
	
	static void case分支判断( int Index, int ControlIndex, int MidVar )
	{
		string Address = null;
		string ConstList = null;
		int CodeNumber = 0;
		int DefaultNumber = 0;
		int CaseNumber = 0;
		int n = int.Parse( ParseNet.NodeSet[ Index ][ 6 ] );
		for( int i = 1; i < ParseNet.NodeSet[ n ].Length; ++i ) {
			int CaseIndex = int.Parse( ParseNet.NodeSet[ n ][ i ] );
			
			if( ParseNet.NodeSet[ CaseIndex ][ 0 ] == ParseNet.Node.default语句 ) {
				DefaultNumber++;
				if( DefaultNumber > 1 ) {
					int dfid = int.Parse( ParseNet.NodeSet[ CaseIndex ][ 1 ] );
					ET.WriteParseError( dfid, "switch语句中最多只能有1个default语句" );
				}
				continue;
			}
			CaseNumber++;
			int ConstIndex = int.Parse( ParseNet.NodeSet[ CaseIndex ][ 2 ] );
			string Const = ConstExpression.GetExpressionValue( ConstIndex, Deploy.UnitName );
			if( Const == null ) {
				Const = "0";
			}
			//int ConstValue = int.Parse( Const );
			
			string label = Config.PreLabel.Case + ControlIndex + "_" + i;
			if( Address == null ) {
				Address = "@code ";
				ConstList = "@code ";
			}
			else {
				Address += ",";
				ConstList += ",";
			}
			Address += "#" + label;
			//Address += "#sys_label6_label";
			ConstList += Const;
			CodeNumber++;
		}
		int swIndex = int.Parse( ParseNet.NodeSet[ Index ][ 1 ] );
		if( CaseNumber == 0 ) {
			ET.WriteParseError( swIndex, "switch语句中至少要有1个case语句" );
			return;
		}
		if( CaseNumber > 255 ) {
			ET.WriteParseError( swIndex, "目前版本语法switch语句中case语句的数量不能大于255个" );
			return;
		}
		//这里可以细化, AVR单片机才需要把flash数组补齐为偶数个
		int cnumber = CodeNumber;
		if( cnumber % 2 != 0 ) {
			cnumber++;
			ConstList += ",0";
		}
		string codetype = VarType.Root + "." + VarType.UserRootUnitName + "0" + ".sys.code";
		
		//创建函数地址列表
		VarNode v = new VarNode();
		string Name = Config.PreLabel.Switchlist + ControlIndex;
		string type = "[" + CodeNumber + " " + codetype + " " + n_Config.Config.GetFunctionPointType();
		v.SetStaticValue( Name, type, n_MemberType.MemberType.VisitType.Private, n_MemberType.MemberType.RealRefType.Real, true, swIndex, 0, 0 );
		v.Address = Address;
		int FuncIdx = VarList.Add( v );
		FuncIdx = Expression.创建引用( FuncIdx );
		
		//如果母函数被优化, 则不分配变量的空间
		v.FunctionIndex = Deploy.FunctionIndex;
		
		//创建常量列表
		VarNode v1 = new VarNode();
		string Name1 = Config.PreLabel.Switchlist + ControlIndex;
		string type1 = "[" + cnumber + " " + codetype + " " + VarType.BaseType.Uint8;
		v1.SetStaticValue( Name1, type1, n_MemberType.MemberType.VisitType.Private, n_MemberType.MemberType.RealRefType.Real, true, swIndex, 0, 0 );
		v1.Address = ConstList;
		int ConstIdx = VarList.Add( v1 );
		ConstIdx = Expression.创建引用( ConstIdx );
		
		//如果母函数被优化, 则不分配变量的空间
		v1.FunctionIndex = Deploy.FunctionIndex;
		
		string labeldefault = Config.PreLabel.Default + ControlIndex;
		if( DefaultNumber == 0 ) {
			labeldefault = Config.PreLabel.End + ControlIndex;
		}
		Expression.AddIns( Code1.分支比较 + " # " + ConstIdx + " " + MidVar + " #" + CodeNumber + " #" + labeldefault );
		Expression.AddIns( Code1.分支散转 + " # " + FuncIdx );
		
		for( int i = 1; i < ParseNet.NodeSet[ n ].Length; ++i ) {
			int CaseIndex = int.Parse( ParseNet.NodeSet[ n ][ i ] );
			
			if( ParseNet.NodeSet[ CaseIndex ][ 0 ] == ParseNet.Node.default语句 ) {
				string label = Config.PreLabel.Default + ControlIndex;
				Expression.AddIns( Code1.标号 + " " + label );
				int CaseSenceIndex = int.Parse( ParseNet.NodeSet[ CaseIndex ][ 3 ] );
				Deploy.语句列表( CaseSenceIndex, ControlIndex );
			}
			else {
				string label = Config.PreLabel.Case + ControlIndex + "_" + i;
				Expression.AddIns( Code1.标号 + " " + label );
				int CaseSenceIndex = int.Parse( ParseNet.NodeSet[ CaseIndex ][ 4 ] );
				Deploy.语句列表( CaseSenceIndex, ControlIndex );
			}
		}
	}
	
	//====================================================================
	
	static void switchelse语句( int Index )
	{
		int ControlIndex = Deploy.SenceNumber;
		
		Expression.AddIns( Code1.语句层加 );
		int ExSenceIndex = int.Parse( ParseNet.NodeSet[ Index ][ 3 ] );
		int VarIndex = 计算整型表达式( ExSenceIndex );
		分支判断( Index, ControlIndex, VarIndex );
		Expression.AddIns( Code1.标号 + " " + Config.PreLabel.End + ControlIndex );
		Expression.AddIns( Code1.语句层减 );
	}
	
	static void 分支判断( int Index, int ControlIndex, int MidVar )
	{
		int n = int.Parse( ParseNet.NodeSet[ Index ][ 6 ] );
		for( int i = 1; i < ParseNet.NodeSet[ n ].Length; ++i ) {
			int CaseIndex = int.Parse( ParseNet.NodeSet[ n ][ i ] );
			
			//获取运算符号
			int OperIndex = int.Parse( ParseNet.NodeSet[ CaseIndex ][ 1 ] );
			string Oper = WordList.GetWord( OperIndex );
			int Exp = Expression.表达式( int.Parse( ParseNet.NodeSet[ CaseIndex ][ 2 ] ) );
			if( Exp == -1 ) {
				return;
			}
			Exp = Expression.读取宏变量( Exp );
			int RetIndex = Expression.双目解析运算( Oper, Exp, MidVar );
			if( RetIndex == -1 ) {
				return;
			}
			RetIndex = Expression.读取宏变量( RetIndex );
			if( MidVar == -1 ) {
				return;
			}
			RetIndex = Expression.读取方法( RetIndex );
			if( MidVar == -1 ) {
				return;
			}
			string MidType = VarList.Get( RetIndex ).Type;
			if( MidType != VarType.BaseType.Bool ) {
				ET.WriteParseError( VarList.Get( RetIndex ).Location, "<分支判断> 表达式应返回判断类型: " + MidType );
			}
			Expression.AddIns( Code1.否定跳转 + " # " + RetIndex + " #" + Config.PreLabel.Case + ControlIndex + "_" + i );
			int CaseSenceIndex = int.Parse( ParseNet.NodeSet[ CaseIndex ][ 4 ] );
			Deploy.语句列表( CaseSenceIndex, ControlIndex );
			Deploy.跳转( Config.PreLabel.End + ControlIndex );
			Expression.AddIns( Code1.标号 + " " + Config.PreLabel.Case + ControlIndex + "_" + i );
		}
		int ElseIndex = int.Parse(ParseNet.NodeSet[ Index ][ 9 ] );
		Deploy.语句列表( ElseIndex, ControlIndex );
	}
	
	//====================================================================
	
	static void if语句( int Index, int CurrentControlIndex )
	{
//		语句层加<<<<
//		有值表达式 2<<<<
//		否定跳转 var? end<<<<
//		带结束语句列表 4<<<<
//		标号 end<<<<
//		语句层减<<<<
		
		int ControlNumber = Deploy.SenceNumber;
		
		Expression.AddIns( Code1.语句层加 );
		int BoolIndex = int.Parse( ParseNet.NodeSet[ Index ][ 3 ] );
		int MidVar = 计算条件表达式( BoolIndex );
		if( MidVar == -1 ) {
			return;
		}
		Expression.AddIns( Code1.否定跳转 + " # " + MidVar + " " + "#" + Config.PreLabel.End + ControlNumber );
		
		int MainSenceIndex = int.Parse( ParseNet.NodeSet[ Index ][ 5 ] );
		Deploy.语句( MainSenceIndex, CurrentControlIndex );
		Expression.AddIns( Code1.标号 + " " + Config.PreLabel.End + ControlNumber );
		Expression.AddIns( Code1.语句层减 );
	}
	
	static void ifelse语句( int Index, int CurrentControlIndex )
	{
//		语句层加<<<<
//		有值表达式 1<<<<
//		否定跳转 var? else<<<<
//		语句列表 3<<<<
//		跳转 end<<<<
//		标号 else<<<<
//		带结束语句列表 5<<<<
//		标号 end<<<<
//		语句层减<<<<
		
		int ControlNumber = Deploy.SenceNumber;
		
		Expression.AddIns( Code1.语句层加 );
		int BoolIndex = int.Parse( ParseNet.NodeSet[ Index ][ 3 ] );
		int MidVar = 计算条件表达式( BoolIndex );
		if( MidVar == -1 ) {
			return;
		}
		Expression.AddIns( Code1.否定跳转 + " # " + MidVar + " " + "#" + Config.PreLabel.Else + ControlNumber );
		
		//这里特殊处理局部变量, 防止在else分支中可见
		//++Deploy.VarLevel;
		int IfSenceIndex = int.Parse( ParseNet.NodeSet[ Index ][ 5 ] );
		Deploy.语句( IfSenceIndex, CurrentControlIndex );
		//--Deploy.VarLevel;
		VarList.ClearUselessVar( Deploy.VarLevel - 1 );
		
		Deploy.跳转( Config.PreLabel.End + ControlNumber );
		Expression.AddIns( Code1.标号 + " " + Config.PreLabel.Else + ControlNumber );
		int ElseSenceIndex = int.Parse( ParseNet.NodeSet[ Index ][ 7 ] );
		Deploy.语句( ElseSenceIndex, CurrentControlIndex );
		Expression.AddIns( Code1.标号 + " " + Config.PreLabel.End + ControlNumber );
		Expression.AddIns( Code1.语句层减 );
	}
	
	static void for语句( int Index )
	{
		int ControlIndex = Deploy.SenceNumber;
		
		//获取主语句索引
		int MainSenceIndex = int.Parse( ParseNet.NodeSet[ Index ][ 9 ] );
		
		Expression.AddIns( Code1.语句层加 );
		
		int InitIndex = int.Parse( ParseNet.NodeSet[ Index ][ 3 ] );
		if( InitIndex != -1 ) {
			Deploy.无值表达式( InitIndex );
		}
		
		Expression.AddIns( Code1.标号 + " " + Config.PreLabel.Start + ControlIndex );
		
		int BoolIndex = int.Parse( ParseNet.NodeSet[ Index ][ 5 ] );
		if( BoolIndex != -1 ) {
			int MidVar = 计算条件表达式( BoolIndex );
			if( MidVar == -1 ) {
				return;
			}
			Expression.AddIns( Code1.否定跳转 + " # " + MidVar + " " + "#" + Config.PreLabel.End + ControlIndex );
		}
		Expression.AddIns( Code1.标号 + " " + Config.PreLabel.Head + ControlIndex );
		Deploy.语句( MainSenceIndex, ControlIndex );
		Expression.AddIns( Code1.标号 + " " + Config.PreLabel.Continue + ControlIndex );
		
		int ExIndex = int.Parse( ParseNet.NodeSet[ Index ][ 7 ] );
		if( ExIndex != -1 ) {
			Deploy.无值表达式( ExIndex );
		}
		Deploy.跳转( Config.PreLabel.Start + ControlIndex );
		
		Expression.AddIns( Code1.标号 + " " + Config.PreLabel.End + ControlIndex );
		
		Expression.AddIns( Code1.语句层减 );
	}
	
	static void loop语句( int Index )
	{
		int ControlIndex = Deploy.SenceNumber;
		
		//获取主语句索引
		int MainSenceIndex = int.Parse( ParseNet.NodeSet[ Index ][ 5 ] );
		
		Expression.AddIns( Code1.语句层加 );
		
		int NumberIndex = int.Parse( ParseNet.NodeSet[ Index ][ 3 ] );
		int MidVar = 计算整型表达式( NumberIndex );
		if( MidVar == -1 ) {
			return;
		}
		//判断是否为用户空间, 是的话需要设置为静态变量
		
		Expression.AddIns( Code1.标号 + " " + Config.PreLabel.Start + ControlIndex );
		
		Expression.AddIns( Code1.跳转递减 + " " + MidVar + " " + MidVar + " #" + Config.PreLabel.End + ControlIndex );
		
		Expression.AddIns( Code1.标号 + " " + Config.PreLabel.Head + ControlIndex );
		Deploy.语句( MainSenceIndex, ControlIndex );
		Expression.AddIns( Code1.标号 + " " + Config.PreLabel.Continue + ControlIndex );
		
		Deploy.跳转( Config.PreLabel.Start + ControlIndex );
		
		Expression.AddIns( Code1.标号 + " " + Config.PreLabel.End + ControlIndex );
		
		Expression.AddIns( Code1.语句层减 );
	}
	
	static void while语句( int Index )
	{
		int ControlIndex = Deploy.SenceNumber;
		
		//获取主语句索引
		int MainSenceIndex = int.Parse( ParseNet.NodeSet[ Index ][ 5 ] );
		
		Expression.AddIns( Code1.语句层加 );
		
		Expression.AddIns( Code1.标号 + " " + Config.PreLabel.Start + ControlIndex );
		
		int BoolIndex = int.Parse( ParseNet.NodeSet[ Index ][ 3 ] );
		if( BoolIndex != -1 ) {
			int MidVar = 计算条件表达式( BoolIndex );
			if( MidVar == -1 ) {
				return;
			}
			Expression.AddIns( Code1.否定跳转 + " # " + MidVar + " " + "#" + Config.PreLabel.End + ControlIndex );
		}
		Expression.AddIns( Code1.标号 + " " + Config.PreLabel.Head + ControlIndex );
		Deploy.语句( MainSenceIndex, ControlIndex );
		Expression.AddIns( Code1.标号 + " " + Config.PreLabel.Continue + ControlIndex );
		
		Deploy.跳转( Config.PreLabel.Start + ControlIndex );
		
		Expression.AddIns( Code1.标号 + " " + Config.PreLabel.End + ControlIndex );
		
		Expression.AddIns( Code1.语句层减 );
	}
	
	static void dowhile语句( int Index )
	{
		int ControlIndex = Deploy.SenceNumber;
		
		//获取主语句索引
		int MainSenceIndex = int.Parse( ParseNet.NodeSet[ Index ][ 2 ] );
		
		Expression.AddIns( Code1.语句层加 );
		
		Expression.AddIns( Code1.标号 + " " + Config.PreLabel.Start + ControlIndex );
		
		Expression.AddIns( Code1.标号 + " " + Config.PreLabel.Head + ControlIndex );
		Deploy.语句( MainSenceIndex, ControlIndex );
		Expression.AddIns( Code1.标号 + " " + Config.PreLabel.Continue + ControlIndex );
		
		int BoolIndex = int.Parse( ParseNet.NodeSet[ Index ][ 5 ] );
		if( BoolIndex != -1 ) {
			int MidVar = 计算条件表达式( BoolIndex );
			if( MidVar == -1 ) {
				return;
			}
			Expression.AddIns( Code1.否定跳转 + " # " + MidVar + " " + "#" + Config.PreLabel.End + ControlIndex );
		}
		Deploy.跳转( Config.PreLabel.Start + ControlIndex );
		
		Expression.AddIns( Code1.标号 + " " + Config.PreLabel.End + ControlIndex );
		
		Expression.AddIns( Code1.语句层减 );
	}
	
	static void repeat语句( int Index )
	{
		int ControlIndex = Deploy.SenceNumber;
		
		int ExSenceIndex = int.Parse( ParseNet.NodeSet[ Index ][ 2 ] );
		int InitIndex = -1;
		int ExIndex = -1;
		int BoolIndex = -1;
		int TimesIndex = -1;
		获取流程辅助语句( ExSenceIndex, ref InitIndex, ref ExIndex, ref BoolIndex, ref TimesIndex );
		
		//获取主语句索引
		int MainSenceIndex = int.Parse( ParseNet.NodeSet[ Index ][ 3 ] );
		
		Expression.AddIns( Code1.语句层加 );
		if( InitIndex != -1 ) {
			Deploy.无值表达式( InitIndex );
		}
		int VarIndex = 计算整型表达式( TimesIndex );
		Expression.AddIns( Code1.标号 + " " + Config.PreLabel.Start + ControlIndex );
		if( BoolIndex != -1 ) {
			int MidVar = 计算条件表达式( BoolIndex );
			if( MidVar == -1 ) {
				return;
			}
			Expression.AddIns( Code1.肯定跳转 + " # " + MidVar + " " + "#" + Config.PreLabel.End + ControlIndex );
		}
		if( VarIndex != -1 ) {
			Expression.AddIns( Code1.跳转递减 + " " + VarIndex + " " + VarIndex + " #" + Config.PreLabel.End + ControlIndex );
		}
		Expression.AddIns( Code1.标号 + " " + Config.PreLabel.Head + ControlIndex );
		Deploy.语句( MainSenceIndex, ControlIndex );
		Expression.AddIns( Code1.标号 + " " + Config.PreLabel.Continue + ControlIndex );
		if( ExIndex != -1 ) {
			Deploy.无值表达式( ExIndex );
		}
		Deploy.跳转( Config.PreLabel.Start + ControlIndex );
		Expression.AddIns( Code1.标号 + " " + Config.PreLabel.End + ControlIndex );
		Expression.AddIns( Code1.语句层减 );
	}
	
	static void once语句( int Index )
	{
//		语句层加<<<<
//		带结束语句列表 2<<<<
//		标号 end<<<<
//		语句层减<<<<
		
		int ControlNumber = Deploy.SenceNumber;
		int ControlIndex = Deploy.SenceNumber;
		
		Expression.AddIns( Code1.语句层加 );
		
		int MainSenceIndex = int.Parse( ParseNet.NodeSet[ Index ][ 2 ] );
		
		Expression.AddIns( Code1.标号 + " " + Config.PreLabel.Head + ControlIndex );
		Deploy.语句( MainSenceIndex, ControlIndex );
		Expression.AddIns( Code1.标号 + " " + Config.PreLabel.Continue + ControlIndex );
		Expression.AddIns( Code1.标号 + " " + Config.PreLabel.End + ControlNumber );
		
		Expression.AddIns( Code1.语句层减 );
	}
	
	static void 用户流程语句( int Index )
	{
//		语句层加<<<<
//		带结束语句列表 2<<<<
//		标号 end<<<<
//		语句层减<<<<
		
		int ControlNumber = Deploy.SenceNumber;
		int ControlIndex = Deploy.SenceNumber;
		
		Expression.AddIns( Code1.语句层加 );
		
		string Name = null;
		int ErrorIndex = -1;
		int NameIndex = int.Parse( ParseNet.NodeSet[ Index ][ 1 ] );
		if( NameIndex != -1 ) {
			n_Parse.Parse.GetMemberNames( Deploy.UnitName, NameIndex, ref Name, ref ErrorIndex );
		}
		if( Name != null ) {
			//处理接口函数调用
			int CallFunctionIndex = n_FunctionList.FunctionList.GetIndex( Name + ".enter" );
			if( CallFunctionIndex == -1 ) {
				ET.WriteParseError( ErrorIndex, "未找到自定义流程的 enter 函数" );
			}
			else {
				Expression.AddIns( Code1.堆栈递增 + " " + "#" + Deploy.FunctionIndex );
				Expression.AddIns( Code1.函数调用 + " " + CallFunctionIndex );
				Expression.AddIns( Code1.堆栈递减 + " " + "#" + Deploy.FunctionIndex );
				n_CallList.CallList.AddSubFunction( Deploy.FunctionIndex, CallFunctionIndex.ToString() );
			}
			Expression.AddIns( Code1.系统标志 + " " + n_Param.Param.Safe );
		}
		
		Expression.AddIns( Code1.标号 + " " + Config.PreLabel.Head + ControlIndex );
		
		//可能不需要判断-1 (不会出现这个情况)
		int MainSenceIndex = int.Parse( ParseNet.NodeSet[ Index ][ 3 ] );
		if( MainSenceIndex != -1 ) {
			Deploy.语句列表( MainSenceIndex, ControlIndex );
		}
		Expression.AddIns( Code1.标号 + " " + Config.PreLabel.Continue + ControlIndex );
		Expression.AddIns( Code1.标号 + " " + Config.PreLabel.End + ControlNumber );
		
		if( Name != null ) {
			//处理接口函数调用
			int CallFunctionIndex = n_FunctionList.FunctionList.GetIndex( Name + ".leave" );
			if( CallFunctionIndex == -1 ) {
				ET.WriteParseError( ErrorIndex, "未找到自定义流程的 leaver 函数" );
			}
			else {
				Expression.AddIns( Code1.堆栈递增 + " " + "#" + Deploy.FunctionIndex );
				Expression.AddIns( Code1.函数调用 + " " + CallFunctionIndex );
				Expression.AddIns( Code1.堆栈递减 + " " + "#" + Deploy.FunctionIndex );
				n_CallList.CallList.AddSubFunction( Deploy.FunctionIndex, CallFunctionIndex.ToString() );
			}
			Expression.AddIns( Code1.系统标志 + " " + n_Param.Param.Unsafe );
		}
		
		Expression.AddIns( Code1.语句层减 );
	}
	
	static void forever语句( int Index )
	{	
		int ControlNumber = Deploy.SenceNumber;
		int ControlIndex = Deploy.SenceNumber;
		
		Expression.AddIns( Code1.语句层加 );
		int MainSenceIndex = int.Parse( ParseNet.NodeSet[ Index ][ 2 ] );
		
		Expression.AddIns( Code1.标号 + " " + Config.PreLabel.Start + ControlIndex );
		Expression.AddIns( Code1.标号 + " " + Config.PreLabel.Head + ControlIndex );
		Deploy.语句( MainSenceIndex, ControlIndex );
		Expression.AddIns( Code1.标号 + " " + Config.PreLabel.Continue + ControlIndex );
		Deploy.跳转( Config.PreLabel.Start + ControlIndex );
		Expression.AddIns( Code1.标号 + " " + Config.PreLabel.End + ControlNumber );
		
		Expression.AddIns( Code1.语句层减 );
	}
	
	static void 获取流程辅助语句( int ExSenceIndex, ref int InitIndex, ref int ExIndex, ref int BoolIndex, ref int TimesIndex )
	{
		int Length = ParseNet.NodeSet[ ExSenceIndex ].Length;
		for( int i = 1; i < Length; ++i ) {
			int ExSenceSubIndex = int.Parse( ParseNet.NodeSet[ ExSenceIndex ][ i ] );
			
			if( ParseNet.NodeSet[ ExSenceSubIndex ][ 0 ] == ParseNet.Node.流程初始化 ) {
				if( InitIndex != -1 ) {
					int ErrIndex = int.Parse( ParseNet.NodeSet[ ExSenceSubIndex ][ 1 ] );
					ET.WriteParseError( ErrIndex, "<获取流程辅助语句> 当前控制语句中已经定义了初始化语句" );
				}
				InitIndex = int.Parse( ParseNet.NodeSet[ ExSenceSubIndex ][ 2 ] );
			}
			else if( ParseNet.NodeSet[ ExSenceSubIndex ][ 0 ] == ParseNet.Node.流程附加操作 ) {
				if( ExIndex != -1 ) {
					int ErrIndex = int.Parse( ParseNet.NodeSet[ ExSenceSubIndex ][ 1 ] );
					ET.WriteParseError( ErrIndex, "<获取流程辅助语句> 当前控制语句中已经定义了附加语句" );
				}
				ExIndex = int.Parse( ParseNet.NodeSet[ ExSenceSubIndex ][ 2 ] );
			}
			else if( ParseNet.NodeSet[ ExSenceSubIndex ][ 0 ] == ParseNet.Node.流程条件判断 ) {
				if( BoolIndex != -1 || TimesIndex != -1 ) {
					int ErrIndex = int.Parse( ParseNet.NodeSet[ ExSenceSubIndex ][ 1 ] );
					ET.WriteParseError( ErrIndex, "<获取流程辅助语句> 当前控制语句中已经定义了条件语句" );
				}
				BoolIndex = int.Parse( ParseNet.NodeSet[ ExSenceSubIndex ][ 2 ] );
			}
			else if( ParseNet.NodeSet[ ExSenceSubIndex ][ 0 ] == ParseNet.Node.流程次数判断 ) {
				if( BoolIndex != -1 || TimesIndex != -1 ) {
					int ErrIndex = int.Parse( ParseNet.NodeSet[ ExSenceSubIndex ][ 2 ] );
					ET.WriteParseError( ErrIndex, "<获取流程辅助语句> 当前控制语句中已经定义了条件语句" );
				}
				TimesIndex = int.Parse( ParseNet.NodeSet[ ExSenceSubIndex ][ 1 ] );
			}
			else {
				ET.ShowError( "<获取流程辅助语句> 未定义的'反复执行'语句附加项: " + ParseNet.NodeSet[ ExSenceSubIndex ][ 0 ] );
			}
		}
	}
	
	static int 计算整型表达式( int ExpIndex )
	{
		if( ExpIndex == -1 ) {
			return -1;
		}
		int MidVar = Expression.表达式( ExpIndex );
		if( MidVar == -1 ) {
			return -1;
		}
		MidVar = Expression.读取宏变量( MidVar );
		if( MidVar == -1 ) {
			return -1;
		}
		MidVar = Expression.读取方法( MidVar );
		if( MidVar == -1 ) {
			return -1;
		}
		string MidType = VarList.Get( MidVar ).Type;
		if( MidType.IndexOf( ',' ) != -1 ) {
			MidType  = MidType.Split( ',' )[ 0 ];
			if( MidType == VarType.BaseType.Bit ) {
				MidType = VarType.BaseType.Uint8;
			}
			VarList.Get( MidVar ).Type = MidType;
		}
		if( !VarType.BaseType.isUint( MidType ) ) {
			//ET.WriteParseError( VarList.Get( MidVar ).Location, "表达式应返回无符号类型: " + MidType );
		}
		if( VarList.Get( MidVar ).ActiveType != "temp" ) {
			VarNode v = new VarNode();
			v.SetTempValue( VarList.Get( MidVar ).Type, Deploy.VarLevel, VarList.Get( MidVar ).Location );
			int vIndex = VarList.Add( v );
			Expression.赋值运算( vIndex, MidVar );
			MidVar = vIndex;
		}
		
		if( n_Param.Param.UserspaceOn ) {
			VarList.Get( MidVar ).ActiveType = n_MemberType.MemberType.StoreType.Static;
			Expression.WriteBack();
		}
		else {
			Expression.WriteBack();
			VarList.Get( MidVar ).ActiveType = "local";
		}
		
		return MidVar;
	}
	
	static int 计算条件表达式( int BoolIndex )
	{
		int MidVar = Expression.表达式( BoolIndex );
		if( MidVar == -1 ) {
			return -1;
		}
		MidVar = Expression.读取宏变量( MidVar );
		if( MidVar == -1 ) {
			return -1;
		}
		MidVar = Expression.读取方法( MidVar );
		if( MidVar == -1 ) {
			return -1;
		}
		string MidType = VarList.Get( MidVar ).Type;
		if( MidType != VarType.BaseType.Bool ) {
			ET.WriteParseError( VarList.Get( MidVar ).Location, "条件表达式应返回判断类型: " + MidType );
			return -1;
		}
		return MidVar;
	}
}
}




